package com.example.administrator.dagger2demo.application;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.util.Log;

import com.alipay.euler.andfix.patch.PatchManager;
import com.example.administrator.dagger2demo.di.ApiModule;
import com.example.administrator.dagger2demo.di.AppComponent;
import com.example.administrator.dagger2demo.di.DaggerAppComponent;
import com.example.administrator.dagger2demo.di.MainModule;
import com.example.administrator.dagger2demo.utils.ImagePipelineConfigFactory;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.stetho.Stetho;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/4/12.
 */
public class DemoApplication extends Application {
    private static final String TAG = "TAG";
    public static String VERSION_NAME = "";
    public static DemoApplication application;
    public static PatchManager mPatchManager;
    private static AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        appComponent = DaggerAppComponent
                .builder()
                .mainModule(new MainModule(this))
                .apiModule(new ApiModule())
                .build();
        Fresco.initialize(getApplicationContext(), ImagePipelineConfigFactory
                .getImagePipelineConfig(getApplicationContext()));
        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);
        //初始化网络调试器 运行app 打开Chrome输入chrome://inspect/#devices
        Stetho.initializeWithDefaults(this);
        try {
            //获取当前版本号
            PackageInfo packageInfo = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
            VERSION_NAME = packageInfo.versionName;
        } catch (Exception e) {
            Log.i(TAG, "e=" + e.toString());
        }

        initAndFix();
    }

    public void initAndFix() {
        //构建热修复管理器
        mPatchManager = new PatchManager(this);
        //初始化热修复器并载入版本号
        mPatchManager.init(VERSION_NAME);
        Log.i(TAG, "init...");
        //加载热修复文件
        mPatchManager.loadPatch();
        Log.i(TAG, "loadPatch...");
    }


    public static AppComponent getAppComponent() {
        return appComponent;
    }


}
